#pragma once

#include "Mutex.h"
#include "Condtion.h"
#include <pthread.h>
#include <functional>
#include <list>
#include <map>
#include <vector>

typedef std::function<void()> Functor;
typedef std::function<void(int, int)> IO_Functor;

class EventPool
{
    EventPool(const EventPool&) = delete;
    EventPool& operator=(const EventPool&) = delete;

public:
    EventPool();
    ~EventPool();

    void Start(int nums);
    void Stop();

    int AddFd(int fd, int events, IO_Functor iocb);
    int ModFd(int fd, int events, IO_Functor iocb);
    void DelFd(int fd);

    void Run(Functor cb);
    void Run(std::list<Functor> cbs);

private:
    void WakeUp();
    void EventHandler(int fd, int events);
    static void *FunctionThread(void *ctx);

private:
    int epollfd_;
    int eventfd_;
    int timerfd_;
    bool loop_;
    Mutex mutex_;
    Condtion cond_;
    Mutex fdMutex_;
    std::map<int, IO_Functor> IOfuctors_;
    std::vector<pthread_t> threads_;
    std::list<Functor> functors_;
};
